//
//  UIColor+JwCate.m
//  JwPack
//
//  Created by 陈警卫 on 2024-07-19.
//

#import "UIColor+JwCate.h"
#import "NSString+JwCate.h"

@implementation UIColor (JwCate)

/// 十六进制颜色
+ (UIColor *)jw_colorHex:(NSInteger)hex alpha:(CGFloat)alpha {
    return [UIColor colorWithRed:((CGFloat)((hex & 0xFF0000) >> 16))/255.0
                           green:((CGFloat)((hex & 0xFF00) >> 8))/255.0
                            blue:((CGFloat)(hex & 0xFF))/255.0 
                           alpha:alpha];
}

/// 十六进制颜色
+ (UIColor *)jw_colorHex:(NSInteger)hex {
    return [UIColor jw_colorHex:hex alpha:1.0];
}

/// 十六进制字符串颜色
+ (UIColor *)jw_colorHexString:(NSString *)hexString alpha:(CGFloat)alpha {
    NSString *colorString = [hexString jw_trimSpace];
    colorString = [[colorString stringByReplacingOccurrencesOfString:@"#" withString:@""] uppercaseString];
    CGFloat red = 0.0;
    CGFloat blue = 0.0;
    CGFloat green = 0.0;
    NSUInteger length = [colorString length];
    if (length != 3 && length != 4 && length != 6 && length != 8) {
        NSLog(@"#UIColor+JwCate# Invalid RGB string, number of characters after '#' should be either 3, 4, 6 or 8");
        return nil;
    }
    unsigned colorValue;
    [[NSScanner scannerWithString:colorString] scanHexInt:&colorValue];
    
    switch ([colorString length]) {
        case 3: // #RGB
            alpha = alpha;
            red   = ((colorValue & 0xF00) >> 8)       / 15.0;
            green = ((colorValue & 0x0F0) >> 4)       / 15.0;
            blue  = ((colorValue & 0x00F))            / 15.0;
            break;
        case 4: // #ARGB
            red   = ((colorValue & 0xF000) >> 12)     / 15.0;
            green = ((colorValue & 0x0F00) >> 8)      / 15.0;
            blue  = ((colorValue & 0x00F0) >> 4)      / 15.0;
            alpha = ((colorValue & 0x000F))           / 15.0;
            break;
        case 6: // #RRGGBB
            red   = ((colorValue & 0xFF0000) >> 16)   / 255.0;
            green = ((colorValue & 0x00FF00) >> 8)    / 255.0;
            blue  = ((colorValue & 0x0000FF))         / 255.0;
            alpha = alpha;
            break;
        case 8: // #RRGGBBAA
            red   = ((colorValue & 0xFF000000) >> 24) / 255.0;
            green = ((colorValue & 0x00FF0000) >> 16) / 255.0;
            blue  = ((colorValue & 0x0000FF00) >> 8)  / 255.0;
            alpha = ((colorValue & 0x000000FF))       / 255.0;
            break;
        default:
            return nil;
            break;
    }
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

/// 十六进制字符串颜色
+ (UIColor *)jw_colorHexString:(NSString *)hexString {
    return [UIColor jw_colorHexString:hexString alpha:1.0];
}

/// 获取红色色值
- (CGFloat)jw_red {
    CGFloat r;
    if ([self getRed:&r green:0 blue:0 alpha:0]) {
        return r;
    }
    return 0;
}
/// 获取绿色色值
- (CGFloat)jw_green {
    CGFloat g;
    if ([self getRed:0 green:&g blue:0 alpha:0]) {
        return g;
    }
    return 0;
}
/// 获取蓝色色值
- (CGFloat)jw_blue {
    CGFloat b;
    if ([self getRed:0 green:0 blue:&b alpha:0]) {
        return b;
    }
    return 0;
}
/// 获取透明色值
- (CGFloat)jw_alpha {
    CGFloat a;
    if ([self getRed:0 green:0 blue:0 alpha:&a]) {
        return a;
    }
    return 0;
}
/// 获取hue（色相）
- (CGFloat)jw_hue {
    CGFloat h;
    if ([self getHue:&h saturation:0 brightness:0 alpha:0]) {
        return h;
    }
    return 0;
}
/// 获取saturation（饱和度）
- (CGFloat)jw_saturation {
    CGFloat s;
    if ([self getHue:0 saturation:&s brightness:0 alpha:0]) {
        return s;
    }
    return 0;
}
/// 获取brightness（亮度）
- (CGFloat)jw_brightness {
    CGFloat b;
    if ([self getHue:0 saturation:0 brightness:&b alpha:0]) {
        return b;
    }
    return 0;
}
/// 剥离掉alpha通道后得到的色值 相当于把当前颜色的半透明值强制设为1.0
- (UIColor *)jw_colorWithoutAlpha {
    CGFloat r;
    CGFloat g;
    CGFloat b;
    if ([self getRed:&r green:&g blue:&b alpha:0]) {
        return [UIColor colorWithRed:r green:g blue:b alpha:1];
    } else {
        return nil;
    }
}

/// 将自身变化到某个目标颜色
- (UIColor *)jw_colorGradualToColor:(UIColor *)toColor progress:(CGFloat)progress {
    return [UIColor jw_colorGradualFromColor:self toColor:toColor progress:progress];
}

/// 将颜色A渐变颜色B progress进度
+ (UIColor *)jw_colorGradualFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor progress:(CGFloat)progress {
    
    progress = progress < 0.0 ? 0.0 : progress;
    progress = progress > 1.0 ? 1.0 : progress;
    
    CGFloat fromRed = fromColor.jw_red;
    CGFloat fromGreen = fromColor.jw_green;
    CGFloat fromBlue = fromColor.jw_blue;
    CGFloat fromAlpha = fromColor.jw_alpha;
    
    CGFloat toRed = toColor.jw_red;
    CGFloat toGreen = toColor.jw_green;
    CGFloat toBlue = toColor.jw_blue;
    CGFloat toAlpha = toColor.jw_alpha;
    
    CGFloat finalRed = fromRed + (toRed - fromRed) * progress;
    CGFloat finalGreen = fromGreen + (toGreen - fromGreen) * progress;
    CGFloat finalBlue = fromBlue + (toBlue - fromBlue) * progress;
    CGFloat finalAlpha = fromAlpha + (toAlpha - fromAlpha) * progress;
    
    return [UIColor colorWithRed:finalRed green:finalGreen blue:finalBlue alpha:finalAlpha];
}

/// 计算两个颜色叠加之后的最终色（注意区分前景色后景色的顺序）
+ (UIColor *)jw_colorFuseWithBackColor:(UIColor *)backColor frontColor:(UIColor *)frontColor {
    
    CGFloat bgAlpha = [backColor jw_alpha];
    CGFloat bgRed = [backColor jw_red];
    CGFloat bgGreen = [backColor jw_green];
    CGFloat bgBlue = [backColor jw_blue];
    
    CGFloat frAlpha = [frontColor jw_alpha];
    CGFloat frRed = [frontColor jw_red];
    CGFloat frGreen = [frontColor jw_green];
    CGFloat frBlue = [frontColor jw_blue];
    
    CGFloat resultAlpha = frAlpha + bgAlpha * (1 - frAlpha);
    CGFloat resultRed = (frRed * frAlpha + bgRed * bgAlpha * (1 - frAlpha)) / resultAlpha;
    CGFloat resultGreen = (frGreen * frAlpha + bgGreen * bgAlpha * (1 - frAlpha)) / resultAlpha;
    CGFloat resultBlue = (frBlue * frAlpha + bgBlue * bgAlpha * (1 - frAlpha)) / resultAlpha;
    
    return [UIColor colorWithRed:resultRed green:resultGreen blue:resultBlue alpha:resultAlpha];
}

/// 获取颜色十六进制值
- (NSString *)jw_hexString {
    return [self jw_hexStringWithAlpha:NO];
}

/// 获取颜色十六进制值
- (NSString *)jw_hexStringWithAlpha {
    return [self jw_hexStringWithAlpha:YES];
}

/// 获取颜色十六进制值
- (NSString *)jw_hexStringWithAlpha:(BOOL)isAlpha {
    CGColorRef color_ref = self.CGColor;
    CGColorSpaceRef color_space = CGColorGetColorSpace(color_ref);
    CGColorSpaceModel color_space_model = CGColorSpaceGetModel(color_space);
    size_t count = CGColorGetNumberOfComponents(color_ref);
    const CGFloat *components = CGColorGetComponents(color_ref);
    
    NSString *hex = nil;
    NSString *alpha = @"";
    
    if (color_space_model == kCGColorSpaceModelMonochrome && count == 2) {
        NSUInteger white = (NSUInteger)(components[0] * 255.0);
        hex = [NSString stringWithFormat:@"%02lX%02lX%02lX", white, white, white];
    }
    if (color_space_model == kCGColorSpaceModelRGB && count == 4) {
        hex = [NSString stringWithFormat:@"%02lX%02lX%02lX",
               (NSUInteger)(components[0] * 255.0),
               (NSUInteger)(components[1] * 255.0),
               (NSUInteger)(components[2] * 255.0)];
        alpha = [NSString stringWithFormat:@"%02lX", 
                 (NSUInteger)(components[3] * 255.0)];
    }
    if (hex && isAlpha) {
        hex = [hex stringByAppendingFormat:@"%@", alpha];
    }
    return hex;
}

/// 适配暗黑模式
+ (UIColor *)jw_colorDynamicWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor {
    if (@available(iOS 13.0, *)) {
        UIColor *color = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull trait) {
            if ([trait userInterfaceStyle] == UIUserInterfaceStyleLight) {
                return lightColor;
            } else if ([trait userInterfaceStyle] == UIUserInterfaceStyleDark) {
                return darkColor;
            } else {
                return lightColor;
            }
        }];
        return color;
    }else{
        return lightColor;
    }
}

/// 适配暗黑模式
+ (UIColor *)jw_colorDynamicWithLightHexString:(NSString *)lightHexString darkHexString:(NSString *)darkHexString {
    UIColor *lightColor = [UIColor jw_colorHexString:lightHexString];
    UIColor *darkColor = [UIColor jw_colorHexString:darkHexString];
    UIColor *color = [self jw_colorDynamicWithLightColor:lightColor darkColor:darkColor];
    return color;
}

/// 适配暗黑模式
+ (UIColor *)jw_colorDynamicWithLightHex:(NSInteger)lightHex darkHex:(NSInteger)darkHex {
    UIColor *lightColor = [UIColor jw_colorHex:lightHex];
    UIColor *darkColor = [UIColor jw_colorHex:darkHex];
    UIColor *color = [self jw_colorDynamicWithLightColor:lightColor darkColor:darkColor];
    return color;
}



@end
